package driver

import (
	"github.com/gin-gonic/gin"
	v1 "golang-sgg-daijia/api/v1"
	"golang-sgg-daijia/internal/handler"
	"golang-sgg-daijia/internal/service"
	"net/http"
)

type MonitorHandler struct {
	*handler.Handler
	monitorService service.MonitorService
}

func NewMonitorHandler(
	handler *handler.Handler,
	monitorService service.MonitorService,
) *MonitorHandler {
	return &MonitorHandler{
		Handler:        handler,
		monitorService: monitorService,
	}
}

// Upload 上传录音
func (h *MonitorHandler) Upload(ctx *gin.Context) {
	file, err := ctx.FormFile("file")
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	req := new(v1.OrderMonitorForm)
	if err := ctx.ShouldBindJSON(req); err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	url, err := h.monitorService.Upload(ctx, file, req)
	if err != nil {
		v1.HandleError(ctx, http.StatusBadRequest, v1.ErrBadRequest, nil)
		return
	}
	v1.HandleSuccess(ctx, url)
}
